/*  CoolJSLib JavaScript - Form, version 0.4
 *  (c) 2010-2012 Andrei Pop, andreipop2005@gmail.com
 *  
 *  Requires: prototype.js 1.7
 *
 *  CoolJSLib is freely distributable under the terms of an MIT-style license.
 *
 *--------------------------------------------------------------------------*/

var FormHelper = {
	
	ChangeBoxType : function(e, newType)
	{
		var input2;
		var err = false;
		
		try // Firefox, Chrome, Safari etc
		{
			input2 = e.cloneNode(false);
			input2.type = newType;
			e.parentNode.replaceChild(input2,e);
		}
		catch (ex)
		{
			err = true;
		}
		
		if (err) // IE
		{
			err = false;
			
			try
			{
				var attr = { 'type': newType, DefaultValue: e.attributes['DefaultValue'].value };

				for (var i=0; i < e.attributes.length; i++)
				{
					var attribute = e.attributes[i].name;
					switch (attribute)
					{
						case 'style':
						case 'class':
						case 'width':
						case 'height':
						case 'tabindex':
						case 'id':
						case 'name':
						{
							attr[attribute] = $(e).readAttribute(attribute);
							break;
						}
					}
				}

				input2 = new Element('input', attr);
				e.parentNode.replaceChild(input2,e);
			}
			catch (ex)
			{
				err = true;
			}
		}
		
		if (err)
		{
			input2 = e;
		}
		
		return input2;
	}
	
};

var FormActions = {

	CanResizeTextBox : false,	// firefox 4
	ScriptDebug : false,
	
	ParseForm : function(formName)
	{
		this.AddDefaultValues(formName);
		
		/*
		$(formName).onsubmit = function()
		{
			alert('a');
		}
		*/
	},
	
	FieldDefaultValues : function(e)
	{
		if (!e.attributes['DefaultValue'] || e.attributes['DefaultValue'].value == '') return;
		
		switch (e.type)
		{
			case 'text':
			case 'textarea':
			{
				if (e.value == "") e.value = e.attributes['DefaultValue'].value;
				
				e.onfocus=function()
				{
					if (this.value == this.attributes['DefaultValue'].value)
					{
						this.value = '';
					}
				};
				
				e.onblur=function()
				{
					if (this.value.trim() == '')
					{
						this.value = this.attributes['DefaultValue'].value;
					}
				};
				
				if (e.type == 'textarea')
				{
					if (!this.CanResizeTextBox)
					{
						e.style['resize'] = 'none';
					}
				}
				
				break;
			}
			
			case 'password':
			{
				e = FormHelper.ChangeBoxType(e, 'text');
				e.value = e.attributes['DefaultValue'].value;
				
				e.onfocus=function()
				{
					var input2 = FormHelper.ChangeBoxType(e, 'password');
					input2.value = '';
					input2.focus();
					
					input2.onblur=function()
					{
						if (this.value.trim() == '')
						{
							FormActions.FieldDefaultValues(this);
						}
					};
				};
				
				break;
			}
			
			default:
			{
				alert('Unsupported object type: ' + e.type);
				
				break;
			}
		}
	},
	
	GetValue : function(e)
	{
		if (!e.attributes['DefaultValue']) return e.value;
		
		switch (e.type)
		{
			case 'text':
			case 'textarea':
			case 'password':
			{
				if (e.value == e.attributes['DefaultValue'].value) return '';
				
				return e.value;
				
				break;
			}
			
			default:
			{
				alert('Unsupported object type: ' + e.type);
				
				break;
			}
		}
	},
	
	AddDefaultValues : function(formName)
	{
		if (!$(formName)) return;
		
		for (var i=0; i < $(formName).elements.length; i++)
		{
			var e = $(formName).elements[i];

			if (e.attributes['DefaultValue'] && e.attributes['DefaultValue'].value != '')
			{
				this.FieldDefaultValues(e);
			}
		}
	},
	
	GetParams: function(Form)
	{
		if (!$(Form)) return false;
		
		var Params='form=' + Form + '&dummy=' + Math.random();
		
		for (i = 0; i < $(Form).elements.length; i++)
		{
			var e = $(Form).elements[i];
			var toAdd = true;
			
			var elemName = e.id;
			var elemValue = e.value;

			if (e.type == 'radio') {
				elemName = e.name;
			}

			if (e.type == 'radio')
			{
				if (!e.checked)
				{
					toAdd = false;
				}
			}
			else if (e.type == 'checkbox')
			{
				if (!e.checked)
				{
					elemValue = 0;
				}
			}
			else if (e.type == 'button')
			{
				toAdd = false;
			}
			
			if (toAdd)
			{
				Params = Params + '&' + elemName + '=' + encodeURIComponent(elemValue);
			}
		}
		
		return Params;
	},
	
	SubmitForm: function(Form, handler, ExtraParams, fail, statusPanel, error_handler)
	{
		var Params = this.GetParams(Form);

		if (!Params) alert('Form "' + Form + '" does not exist!');
		
		if (ExtraParams) Params=Params + '&' + ExtraParams;
		
		SendAjaxRequest(Params, handler, fail, statusPanel, error_handler);
	}
	
};
